package com.yantong.service.impl;

import com.yantong.pojo.CentreIndustry;
import com.yantong.mapper.CentreIndustryMapper;
import com.yantong.service.CentreIndustryService;
import com.yantong.constant.BusinessCode;
import com.yantong.exception.BDException;
import com.yantong.utils.PageUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *  商户行业CentreInDustryServiceImpl
 *
 * @author huangshuai-728982210@qq.com
 * @date 2021/1/18 9:37
 */
@Service
public class CentreIndustryServiceImpl implements CentreIndustryService {

	@Autowired
	CentreIndustryMapper centreIndustryMapper;

	@Override
	public int saveCentreIndustry(CentreIndustry centreIndustry) {
		if (centreIndustry == null) {
			return 0;
		}
		int total = centreIndustryMapper.countById(centreIndustry.getId());
		if (total > 0) {
			throw new BDException(BusinessCode.CENTRE_TERMINAL_EXIST);
		}
		return centreIndustryMapper.insertSelective(centreIndustry);
	}

	@Override
	public PageUtils<CentreIndustry> listCentreIndustry(CentreIndustry centreIndustry, int offset, int limit) {
		PageUtils<CentreIndustry> pageUtils = new PageUtils<>();
		int total = centreIndustryMapper.selectByCount();
		if (total > 0) {
			pageUtils.setTotal(total);
			pageUtils.setRows(centreIndustryMapper.listByAll(centreIndustry,new RowBounds(offset, limit)));
		}
		return pageUtils;
	}

	@Override
	public int updateCentreIndustry(CentreIndustry centreIndustry) {
		if (centreIndustry == null || centreIndustry.getId() == null) {
			return 0;
		}
		return centreIndustryMapper.updateByPrimaryKeySelective(centreIndustry);
	}

	@Override
	public CentreIndustry getCentreIndustryById(Long id) {
		if (id == null) {
			return null;
		}
		return centreIndustryMapper.selectByPrimaryKey(id);
	}
}
